
Page({
    data: {
        heros: [] as Hero[],
        name: ''
    },
    async onLoad() {
        this.getHeros()
        .then((data)=>{
            this.setData({
                heros:data
            })
        })
    },
    //搜索提交
   async onSubmit(){
      const list = await  this.getHeros(this.data.name)
      if(list?.length>0){
        this.setData({
            heros:list
        })
      }else{
        wx.showToast({title:'查无此人',icon:'error'})
    }
     
    },
    //获取英雄详情
    //q为英雄的名称
    getHeros(q: string='' ) {
        wx.showLoading({title:'数据加载中……'})
        return new Promise<Hero[]>((resolve, reject) => {
            wx.request<ServerResponse<Hero[]>>({
                method: 'GET',
                url: 'https://api-vue-base.itheima.net/api/lol/search',
                data: {
                    // 若q：q，则q可以省略
                    q
                },
                success: (res) => {
                    resolve(res.data.data)
                },
                fail: () => {
                    reject()
                },
                complete:()=>{
                    wx.hideLoading()
                }
            })
        })
    },
})